﻿using System;
using System.Collections.Generic;
using System.Text;
using Camstar.XMLClient.API;
using MES.DataGateway.Common;
using MES.DataGateway.SyncEntity;
using System.Data;
using System.Windows.Forms;
using System.Configuration;
using External.APICalling;
using BusinessRule.DTO;
using MES.DataGateway.OracleDAL;


namespace MES.DataGateway.SyncCommon
{
    public class ExecuteAPI
    {


        private string gHost = "127.0.0.1";
        private int gPort = 2881;
        private string gUserName = "18707528";
        private string gPassword = "Abc123456789";
        private string m_OperName = "下料";
        private StandardDAL m_DAL = new StandardDAL();

        public ExecuteAPI()
        {
            CurrencyClass common = new CurrencyClass();
            common.GetConfigSetting(ref gHost, ref gPort, ref gUserName, ref gPassword);
            m_OperName = ConfigurationManager.AppSettings["OperName"].Trim();
        }
        /// <summary>
        /// 填充API实例
        /// </summary>
        /// <param name="m_DataList"></param>
        /// <param name="tmpEntity"></param>
        private static void FillAPIEntity(List<ClientAPIEntity> m_DataList, DataEntity tmpEntity)
        {
            if (tmpEntity.FieldType == "dataField")
                m_DataList.Add(new ClientAPIEntity(tmpEntity.FieldName,  DataTypeEnum.DataField, tmpEntity.FieldValue, ""));
            else if (tmpEntity.FieldType == "namedObjectField")
                m_DataList.Add(new ClientAPIEntity(tmpEntity.FieldName,  DataTypeEnum.NamedObjectField, tmpEntity.FieldValue, ""));
            else if (tmpEntity.FieldType == "revisionedObjectField")
                m_DataList.Add(new ClientAPIEntity(tmpEntity.FieldName,  DataTypeEnum.RevisionedObjectField, tmpEntity.FieldValue,tmpEntity .FileldVersion ));
            else if (tmpEntity.FieldType == "namedObjectList")
                m_DataList.Add(new ClientAPIEntity(tmpEntity.FieldName, DataTypeEnum.NamedObjectList , tmpEntity.FieldValue,"","0",false));
      
        
        }

        /// <summary>
        /// 填充WorkflowAPI实例
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="stepList"></param>
        /// <param name="specList"></param>
        /// <param name="strWorkflowName"></param>
        /// <param name="strRevsion"></param>
        /// <param name="m_DataList"></param>
        /// <returns></returns>
        private static DataTable FillWorkflowAPIEntity(SyncParameterEntity mainEntity, List<DataEntity> stepList, List<DataEntity> specList, ref string strWorkflowName, ref string strRevsion, List<ClientAPIEntity> m_DataList)
        {
            foreach (DataEntity entity in mainEntity.DataList)
            {

                if (entity.FieldName == "Name")
                    strWorkflowName = entity.FieldValue.ToString();
                if (entity.FieldName == "Revision")
                    strRevsion = entity.FieldValue.ToString();

                FillAPIEntity(m_DataList, entity);
            }
            DataTable dtSteplist = new DataTable();
            dtSteplist.Columns.Add("StepName");
            dtSteplist.Columns.Add("Description");
            dtSteplist.Columns.Add("ObjName");
            dtSteplist.Columns.Add("ObjRev");
            dtSteplist.Columns.Add("ObjType");

            for (int i = 0; i < stepList.Count; i++)
            {
                DataEntity tmpEntity = stepList[i];
                DataRow stepdr = dtSteplist.NewRow();
                stepdr["StepName"] = tmpEntity.FieldValue;
                stepdr["Description"] = "";
                for (int j = 0; j < specList.Count; j++)
                {
                    DataEntity specEntity = specList[j];
                    if (specEntity.IndexNo == i)
                    {
                        stepdr["ObjType"] = specEntity.FieldName;
                        stepdr["ObjName"] = specEntity.FieldValue.ToString();
                        stepdr["ObjRev"] = specEntity.FileldVersion.ToString();
                        dtSteplist.Rows.Add(stepdr);
                        break;
                    }
                }
            }
            return dtSteplist;
        }
        /// <summary>
        /// 创建工艺路线
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="stepList"></param>
        /// <param name="specList"></param>
        /// <param name="outEntity"></param>
        public void CreateNewWorkFlow(SyncParameterEntity mainEntity, List<DataEntity> stepList, List<DataEntity> specList, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "MachineMfgWorkFlow";
            string strWorkflowName, strRevsion, strMsg;
            strWorkflowName = strRevsion = strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            DataTable dtSteplist = FillWorkflowAPIEntity(mainEntity, stepList, specList, ref strWorkflowName, ref strRevsion, m_DataList);

           bool isSuccess = api.CreateWorkflow(outEntity.StrServicekey, ref strWorkflowName, ref strRevsion, m_DataList, dtSteplist, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strWorkflowName + ",版本:" + strRevsion+"工艺路线创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");
        }

        /// <summary>
        /// 创建工艺版本
        /// </summary>
        /// <param name="strOldRevsion"></param>
        /// <param name="strOldWorkFlowName"></param>
        /// <param name="mainEntity"></param>
        /// <param name="stepList"></param>
        /// <param name="specList"></param>
        /// <param name="outEntity"></param>
        public void CreateNewWorkFlowRevision(string strOldRevsion, string strOldWorkFlowName, SyncParameterEntity mainEntity,
                                                List<DataEntity> stepList, List<DataEntity> specList, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "MachineMfgWorkFlow";
            string strWorkflowName, strRevsion, strMsg;
            strWorkflowName = strRevsion = strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            DataTable dtSteplist = FillWorkflowAPIEntity(mainEntity, stepList, specList, ref strWorkflowName, ref strRevsion, m_DataList);

            m_DataList.Add(new ClientAPIEntity("IsRevOfRcd", InputTypeEnum.Details, DataTypeEnum.DataField, "1", ""));           
            bool isSuccess = api.CreateWorkflowRevsion(outEntity.StrServicekey, ref strWorkflowName, ref strOldRevsion, ref strRevsion, m_DataList, dtSteplist, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strWorkflowName + ",版本:" + strRevsion + "工艺路线版本创建成功";
                outEntity.OutFlag = 1;
                //暂停相关批次
                //m_DAL.suspendContainer("", "", strWorkflowName, strRevsion);
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }
        /// <summary>
        /// 创建工序模板
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateSpec(SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "Spec";
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }
            m_DataList.Add(new ClientAPIEntity("Operation", InputTypeEnum.Details, DataTypeEnum.NamedObjectField, m_OperName, ""));
            m_DataList.Add(new ClientAPIEntity("IsRevOfRcd", InputTypeEnum.Details, DataTypeEnum.DataField, "1", ""));

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = "工序模板创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");
        }
        /// <summary>
        /// 创建工序模板版本
        /// </summary>
        /// <param name="strOldSpecName"></param>
        /// <param name="strOldRevsion"></param>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateSpecVersion(string strOldSpecName, string strOldRevsion, SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "Spec";
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }
            m_DataList.Add(new ClientAPIEntity("Operation", InputTypeEnum.Details, DataTypeEnum.NamedObjectField, m_OperName, ""));
            m_DataList.Add(new ClientAPIEntity("IsRevOfRcd", InputTypeEnum.Details, DataTypeEnum.DataField, "1", ""));

            bool isSuccess = api.CreateNewModelRevision(outEntity.StrServicekey + "Maint", strOldSpecName, strOldRevsion, m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = "工序模板版本创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");
        }

        /// <summary>
        /// 填充BOMAPI实例
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="productList"></param>
        /// <param name="m_DataList"></param>
        /// <param name="subentityList"></param>
        /// <param name="productmaterialList"></param>
        private static void FillBomAPIEntity(SyncParameterEntity mainEntity, List<SyncParameterEntity> productList, List<ClientAPIEntity> m_DataList, Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> subentityList, Dictionary<string, List<ClientAPIEntity>> productmaterialList)
        {
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }

            for (int i = 0; i < productList.Count; i++)
            {
                SyncParameterEntity myEntity = productList[i];
                List<ClientAPIEntity> productmaterial = new List<ClientAPIEntity>();
                for (int j = 0; j < myEntity.DataList.Count; j++)
                {
                    FillAPIEntity(productmaterial, myEntity.DataList[j]);
                }
                productmaterialList.Add(i.ToString(), productmaterial);
            }
            subentityList.Add("MaterialList", productmaterialList);
            m_DataList.Add(new ClientAPIEntity("IsRevOfRcd", InputTypeEnum.Details, DataTypeEnum.DataField, "1", ""));
        }
        /// <summary>
        /// 创建BOM
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="productList"></param>
        /// <param name="outEntity"></param>
        public void CreateBom(SyncParameterEntity mainEntity, List<SyncParameterEntity> productList, ref OutInfoEntity outEntity)
        {

            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "BOM";
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> subentityList = new Dictionary<string, Dictionary<string, List<ClientAPIEntity>>>();
            Dictionary<string, List<ClientAPIEntity>> productmaterialList = new Dictionary<string, List<ClientAPIEntity>>();
            FillBomAPIEntity(mainEntity, productList, m_DataList, subentityList, productmaterialList);

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, subentityList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = "BOM创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }

        public void CreateBomVersion(string oldBomName, string oldVersion, SyncParameterEntity mainEntity, List<SyncParameterEntity> productList, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "BOM";
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> subentityList = new Dictionary<string, Dictionary<string, List<ClientAPIEntity>>>();
            Dictionary<string, List<ClientAPIEntity>> productmaterialList = new Dictionary<string, List<ClientAPIEntity>>();
            FillBomAPIEntity(mainEntity, productList, m_DataList, subentityList, productmaterialList);

            bool isSuccess = api.CreateNewModelRevision(outEntity.StrServicekey + "Maint", oldBomName, oldVersion, m_DataList, subentityList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = "BOM版本创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }
        /// <summary>
        /// WriteBy: YangSJ 2017-7-22
        /// 子工艺BOM
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="productList"></param>
        /// <param name="outEntity"></param>
        public void CreateWorkflowBom(SyncParameterEntity mainEntity, List<SyncParameterEntity> productList, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "WorkflowBOM";
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> subentityList = new Dictionary<string, Dictionary<string, List<ClientAPIEntity>>>();
            Dictionary<string, List<ClientAPIEntity>> productmaterialList = new Dictionary<string, List<ClientAPIEntity>>();
            FillBomAPIEntity(mainEntity, productList, m_DataList, subentityList, productmaterialList);

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, subentityList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = "WorkflowBOM创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }
        /// <summary>
        /// WriteBy: YangSJ 2017-7-22
        /// 子工艺BOM创建版本
        /// </summary>
        /// <param name="oldBomName"></param>
        /// <param name="oldVersion"></param>
        /// <param name="mainEntity"></param>
        /// <param name="productList"></param>
        /// <param name="outEntity"></param>
        public void CreateWorkflowBomVersion(string oldBomName, string oldVersion, SyncParameterEntity mainEntity, List<SyncParameterEntity> productList, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "WorkflowBOM";
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> subentityList = new Dictionary<string, Dictionary<string, List<ClientAPIEntity>>>();
            Dictionary<string, List<ClientAPIEntity>> productmaterialList = new Dictionary<string, List<ClientAPIEntity>>();
            FillBomAPIEntity(mainEntity, productList, m_DataList, subentityList, productmaterialList);

            bool isSuccess = api.CreateNewModelRevision(outEntity.StrServicekey + "Maint", oldBomName, oldVersion, m_DataList, subentityList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = "WorkflowBOM版本创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }
        /// <summary>
        /// 创建NDO仅名称
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="strValue"></param>
        /// <param name="outEntity"></param>
        public void CreateNDOData(string strName, string strValue, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strName;
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            m_DataList.Add(new ClientAPIEntity("Name", InputTypeEnum.Details, DataTypeEnum.DataField, strValue, ""));

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strName + "创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }
        /// <summary>
        /// 创建NDO对象实例
        /// </summary>
        /// <param name="strTxnName"></param>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateNDOEntity(string strTxnName, SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strTxnName;
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strTxnName + "创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }

        /// <summary>
        /// 创建NDO对象实例
        /// </summary>
        /// <param name="strTxnName"></param>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateNDOEntity(string strTxnName, SyncParameterEntity mainEntity, Dictionary<string, Dictionary<string, List<DataEntity>>> subentityList, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strTxnName;
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> _subentityList = null;
            FillSubentityList(subentityList,ref _subentityList);

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, _subentityList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strTxnName + "创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }
       
        /// <summary>
        /// 更新NDO实例
        /// </summary>
        /// <param name="strTxnName"></param>
        /// <param name="objectname"></param>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        public void UpdateNDOEntity(string strTxnName, string objectname, SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strTxnName;
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }

            bool isSuccess = api.UpdateModel(outEntity.StrServicekey + "Maint", objectname, null, m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strTxnName + "修改成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }

        public void SaveAsEntity(string strTxnName, string srcName, string srcVersion, SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strTxnName;
            string strMsg = string.Empty;

            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }
            //m_DataList.Add(new ClientAPIEntity("Name", InputTypeEnum.Details, DataTypeEnum.DataField, copyName, ""));
            //if (!string.IsNullOrWhiteSpace(copyVersion))
            //    m_DataList.Add(new ClientAPIEntity("Revision", InputTypeEnum.Details, DataTypeEnum.DataField, copyVersion, ""));


            bool isSuccess = api.SaveAsModel(outEntity.StrServicekey + "Maint", srcName, srcVersion, m_DataList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strTxnName + "复制成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");
        }

        /// <summary>
        /// 复制产品
        /// </summary>
        /// <param name="strList"></param>
        /// <param name="strInfo"></param>
        /// <returns></returns>
        public Boolean CopyProduct(Dictionary<string, string> strList, ref string strInfo)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);

            List<ClientAPIEntity> dataEntityList = new List<ClientAPIEntity>();
            ClientAPIEntity dataEntity;

            dataEntity = new ClientAPIEntity();
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField;
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details;
            dataEntity.ClientDataName = "Name";
            dataEntity.ClientDataValue = strList["productName"];
            dataEntityList.Add(dataEntity);

            dataEntity = new ClientAPIEntity();
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField;
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details;
            dataEntity.ClientDataName = "Revision";
            dataEntity.ClientDataValue = strList["Revision"];
            dataEntityList.Add(dataEntity);

            if (strList.ContainsKey("ProductType"))
            {
                dataEntity = new ClientAPIEntity();
                dataEntity.ClientDataTypeEnum = DataTypeEnum.NamedObjectField;
                dataEntity.ClientInputTypeEnum = InputTypeEnum.Details;
                dataEntity.ClientDataName = "ProductType";
                dataEntity.ClientDataValue = strList["ProductType"];
                dataEntityList.Add(dataEntity);
            }
            if (strList.ContainsKey("MaterialCode"))
            {
                dataEntity = new ClientAPIEntity();
                dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField ;
                dataEntity.ClientInputTypeEnum = InputTypeEnum.Details;
                dataEntity.ClientDataName = "MaterialCode";
                dataEntity.ClientDataValue = strList["MaterialCode"];
                dataEntityList.Add(dataEntity);
            }

            Boolean result = api.SaveAsModel("ProductMaint", strList["PdmProductName"], strList["Revision"], dataEntityList, ref strInfo);

            return result;

        }

        
        /// <summary>
        /// 创建RDO实例
        /// </summary>
        /// <param name="strTxnName"></param>
        /// <param name="productEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateRDOEntity(string strTxnName, SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CreateNDOEntity(strTxnName, mainEntity, ref outEntity);
        }

        /// <summary>
        /// 创建RDO实例
        /// </summary>
        /// <param name="strTxnName"></param>
        /// <param name="productEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateRDOEntity(string strTxnName, SyncParameterEntity mainEntity, Dictionary<string, Dictionary<string, List<DataEntity>>> subentityList, ref OutInfoEntity outEntity)
        {
            CreateNDOEntity(strTxnName, mainEntity,subentityList, ref outEntity);
        }
        /// <summary>
        /// 创建RDO实例版本
        /// </summary>
        /// <param name="productName"></param>
        /// <param name="oldRevision"></param>
        /// <param name="productEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateRDOEntityRevision(string strTxnName, string objectname, string oldRevision, SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strTxnName;
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();

            string revision = "";
            foreach (DataEntity entity in mainEntity.DataList)
            {
                if (entity.FieldName == "Revision")
                    revision = entity.FieldValue.ToString();
                FillAPIEntity(m_DataList, entity);
            }
            m_DataList.Add(new ClientAPIEntity("IsRevOfRcd", InputTypeEnum.Details, DataTypeEnum.DataField, "1", ""));

            bool isSuccess = api.CreateNewModelRevision(outEntity.StrServicekey + "Maint", objectname, oldRevision, m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = outEntity.StrServicekey + "创建成功";
                outEntity.OutFlag = 1;

                //暂停相关批次
                if (strTxnName == "Product") {
                   // m_DAL.suspendContainer(objectname, revision,"","");
                }
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");
        }
        /// <summary>
        /// 创建RDO实例版本
        /// </summary>
        /// <param name="productName"></param>
        /// <param name="oldRevision"></param>
        /// <param name="productEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateRDOEntityRevision(string strTxnName, string objectname, string oldRevision, SyncParameterEntity mainEntity, Dictionary<string, Dictionary<string, List<DataEntity>>> subentityList , ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strTxnName;
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();

            string revision = "";
            foreach (DataEntity entity in mainEntity.DataList)
            {
                if (entity.FieldName == "Revision")
                    revision = entity.FieldValue.ToString();
                FillAPIEntity(m_DataList, entity);
            }
            m_DataList.Add(new ClientAPIEntity("IsRevOfRcd", InputTypeEnum.Details, DataTypeEnum.DataField, "1", ""));
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> _subentityList=null;
            FillSubentityList(subentityList, ref _subentityList);
            bool isSuccess = api.CreateNewModelRevision(outEntity.StrServicekey + "Maint", objectname, oldRevision, m_DataList, _subentityList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = outEntity.StrServicekey + "创建成功";
                outEntity.OutFlag = 1;

                //暂停相关批次
                if (strTxnName == "Product")
                {
                    //m_DAL.suspendContainer(objectname, revision, "", "");
                }
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");
        }

        private static void FillSubentityList(Dictionary<string, Dictionary<string, List<DataEntity>>> subentityList,ref Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> _subentityList)
        {
            if (subentityList == null)
                return;
            if (subentityList.Count == 0)
            {
                _subentityList = null;
                return;
            }
            _subentityList = new Dictionary<string, Dictionary<string, List<ClientAPIEntity>>>();
            foreach (string key in subentityList.Keys)
            {
                Dictionary<string, List<ClientAPIEntity>> AttributesList = new Dictionary<string, List<ClientAPIEntity>>();
                foreach (string _key in subentityList[key].Keys)
                {
                    List<ClientAPIEntity> AttributesItem = new List<ClientAPIEntity>();
                    foreach (DataEntity entity in subentityList[key][_key])
                    {
                        FillAPIEntity(AttributesItem, entity);
                    }
                    AttributesList.Add("0", AttributesItem);
                }
                _subentityList.Add(key, AttributesList);
            }

        }
      
        /// <summary>
        /// 更新RDO实例
        /// </summary>
        /// <param name="productName"></param>
        /// <param name="revision"></param>
        /// <param name="productEntity"></param>
        /// <param name="outEntity"></param>
        public void UpdateRDOEntity(string strTxnName, string objectname, string oldRevision, SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strTxnName;
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }

            bool isSuccess = api.UpdateModel(outEntity.StrServicekey + "Maint", objectname, oldRevision, m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strTxnName + "修改成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }
        /// <summary>
        /// 更新RDO实例
        /// </summary>
        /// <param name="productName"></param>
        /// <param name="revision"></param>
        /// <param name="productEntity"></param>
        /// <param name="outEntity"></param>
        public void UpdateRDOEntity(string strTxnName, string objectname, string oldRevision, SyncParameterEntity mainEntity, Dictionary<string, Dictionary<string, List<DataEntity>>> subentityList, int oldSubentityListCount, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = strTxnName;
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                FillAPIEntity(m_DataList, entity);
            }
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> _subentityList = null;
            FillSubentityList(subentityList, ref _subentityList);
            string strname="";
            Dictionary<string, List<ClientAPIEntity>> dict = new Dictionary<string, List<ClientAPIEntity>>();
            if (_subentityList != null){
                foreach (string str in _subentityList.Keys)
                {
                    strname = str;
                    dict = _subentityList[str];
                    break;
                }
            }
            
            bool isSuccess=false ;
            if (dict.Count >0)
                isSuccess = api.UpdateModelHasSubentityList(outEntity.StrServicekey + "MaintDoc", outEntity.StrServicekey + "Maint", objectname, oldRevision, "RO", m_DataList, strname, dict, oldSubentityListCount, ref strMsg);
            else
                isSuccess = api.UpdateModel(outEntity.StrServicekey + "Maint", objectname, oldRevision, m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = strTxnName + "修改成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

        }
     
        /// <summary>
        /// 创建文档
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        /// <returns></returns>
        public Dictionary<string, string> CreateDocument(SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "Document";
            string strMsg = string.Empty;
            string DocumentName = "";
            string DocumentRev = "";
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                if (entity.FieldName.ToUpper() == "NAME")
                {
                    DocumentName = entity.FieldValue.ToString().Trim();
                }
                else if (entity.FieldName.ToUpper() == "REVISION")
                {
                    DocumentRev = entity.FieldValue.ToString().Trim();
                }

                FillAPIEntity(m_DataList, entity);
            }
            m_DataList.Add(new ClientAPIEntity("IsRevOfRcd", InputTypeEnum.Details, DataTypeEnum.DataField, "1", ""));

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = outEntity.StrServicekey + "创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

            Dictionary<string, string> NameAndRev = new Dictionary<string, string>();
            if (string.IsNullOrWhiteSpace(DocumentName))
                NameAndRev.Add(DocumentName, DocumentRev);
            return NameAndRev;

        }
        /// <summary>
        /// 创建文档版本
        /// </summary>
        /// <param name="objectname"></param>
        /// <param name="oldRevsion"></param>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        /// <returns></returns>
        public Dictionary<string, string> CreateDocumentVersion(string objectname, string oldRevsion, SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "Document";
            string strMsg = string.Empty;
            string DocumentName = "";
            string DocumentRev = "";
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            foreach (DataEntity entity in mainEntity.DataList)
            {
                if (entity.FieldName.ToUpper() == "NAME")
                {
                    DocumentName = entity.FieldValue.ToString().Trim();
                }
                else if (entity.FieldName.ToUpper() == "REVISION")
                {
                    DocumentRev = entity.FieldValue.ToString().Trim();
                }

                FillAPIEntity(m_DataList, entity);
            }
            m_DataList.Add(new ClientAPIEntity("IsRevOfRcd", InputTypeEnum.Details, DataTypeEnum.DataField, "1", ""));

            bool isSuccess = api.CreateNewModelRevision(outEntity.StrServicekey + "Maint", objectname, oldRevsion, m_DataList, null, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = outEntity.StrServicekey + "创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");

            Dictionary<string, string> NameAndRev = new Dictionary<string, string>();
            if (string.IsNullOrWhiteSpace(DocumentName))
                NameAndRev.Add(DocumentName, DocumentRev);
            return NameAndRev;

        }
        /// <summary>
        /// 创建文档集
        /// </summary>
        /// <param name="strDocumentSetName"></param>
        /// <param name="NameAndRev"></param>
        /// <param name="outEntity"></param>
        public void CreateDocumentSet(string strDocumentSetName, Dictionary<string, string> DocList, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "DocumentSet";
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            m_DataList.Add(new ClientAPIEntity("Name", InputTypeEnum.Details, DataTypeEnum.DataField, strDocumentSetName, ""));
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> subentityList = new Dictionary<string, Dictionary<string, List<ClientAPIEntity>>>();
            Dictionary<string, List<ClientAPIEntity>> ObjectList = new Dictionary<string, List<ClientAPIEntity>>();
            int idx = 0;
            foreach (KeyValuePair<string, string> item in DocList)
            {
                string ObjName = idx + item.Key;
                List<ClientAPIEntity> objEntity = new List<ClientAPIEntity>();
                objEntity.Add(new ClientAPIEntity("Name", InputTypeEnum.Details, DataTypeEnum.DataField, ObjName, ""));
                objEntity.Add(new ClientAPIEntity("Document", InputTypeEnum.Details, DataTypeEnum.RevisionedObjectField, item.Key, item.Value));
                idx += 1;
                ObjectList.Add(idx.ToString(), objEntity);
            }
            subentityList.Add("DocumentEntries", ObjectList);

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, subentityList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = outEntity.StrServicekey + "创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");
        }

        /// <summary>
        /// 创建实例SpecialTool
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateXASpecialTool(SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CreateNDOEntity("SpecialTool", mainEntity, ref outEntity);
        }
        /// <summary>
        /// 创建实例CommonTool
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateXACommonTool(SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CreateNDOEntity("CommonTool", mainEntity, ref outEntity);
        }
        /// <summary>
        /// 创建实例Resource
        /// </summary>
        /// <param name="mainEntity"></param>
        /// <param name="outEntity"></param>
        public void CreateResource(SyncParameterEntity mainEntity, ref OutInfoEntity outEntity)
        {
            CreateNDOEntity("Resource", mainEntity, ref outEntity);
        }

        /// <summary>
        /// 创建实例ResourceGroup
        /// </summary>
        /// <param name="ResDT"></param>
        /// <param name="GroupName"></param>
        /// <param name="outEntity"></param>
        public void CreateResourceGroup(DataTable ResDT, string GroupName, ref OutInfoEntity outEntity)
        {
            CamstarClientAPI api = new CamstarClientAPI(gUserName, gPassword);
            outEntity.StrServicekey = "ResourceGroup";
            string strMsg = string.Empty;
            List<ClientAPIEntity> m_DataList = new List<ClientAPIEntity>();
            m_DataList.Add(new ClientAPIEntity("Name", InputTypeEnum.Details, DataTypeEnum.DataField, GroupName, ""));
            Dictionary<string, Dictionary<string, List<ClientAPIEntity>>> subentityList = new Dictionary<string, Dictionary<string, List<ClientAPIEntity>>>();
            Dictionary<string, List<ClientAPIEntity>> ObjectList = new Dictionary<string, List<ClientAPIEntity>>();

            for (int i = 0; i < ResDT.Rows.Count; i++)
            {
                List<ClientAPIEntity> objEntity = new List<ClientAPIEntity>();
                objEntity.Add(new ClientAPIEntity("Name", InputTypeEnum.Details, DataTypeEnum.DataField, ResDT.Rows[i]["name"].ToString(), ""));
                ObjectList.Add(i.ToString(), objEntity);
            }
            subentityList.Add("Entries", ObjectList);

            bool isSuccess = api.CreateModel(outEntity.StrServicekey + "Maint", m_DataList, subentityList, ref strMsg);
            outEntity.StrInfo = strMsg;
            if (isSuccess)
            {
                outEntity.StrInfo = outEntity.StrServicekey + "创建成功";
                outEntity.OutFlag = 1;
            }
            else
            {
                outEntity.OutFlag = 2;
            }
            Log.WriteLog(outEntity.StrInfo, 0, "服务调用");
        }

        /// <summary>
        /// 创建辅料,自己分割型
        /// </summary>
        /// <param name="strinfo"></param>
        /// <returns></returns>
        public string CreateAuxMaterial(string[] strinfo)
        {
            //"材料编码$名称$规格$牌号$技术标准$数量$备注"
            string toolID = "";
            string cdoName = "AuxMaterial";
            string cdoFieldName = "AuxMaterialName";
            string cdoFieldID = "AuxMaterialID";
            string name = strinfo[0].Trim();
            SyncParameterEntity inputParaEntity = new SyncParameterEntity();
            StandardDAL m_DAL = new StandardDAL();
            DataSet ds = m_DAL.GetNDOTable(cdoName, cdoFieldName, name);
            if (ds.Tables[0].Rows.Count > 0)
            {
                toolID = ds.Tables[0].Rows[0][cdoFieldID].ToString();
            }
            else
            { 
                #region 属性赋值
                inputParaEntity.DataList.Add(new DataEntity("Name", "dataField", name, ""));
                inputParaEntity.DataList.Add(new DataEntity("Description", "dataField", strinfo[1].Trim(), ""));
                inputParaEntity.DataList.Add(new DataEntity("Specification", "dataField", strinfo[2].Trim(), ""));
                inputParaEntity.DataList.Add(new DataEntity("VendorSerialNumber", "dataField", strinfo[3].Trim(), ""));
                inputParaEntity.DataList.Add(new DataEntity("TechnicalStandard", "dataField", strinfo[4].Trim(), ""));
                inputParaEntity.DataList.Add(new DataEntity("Notes", "dataField", strinfo[6].Trim(), ""));
                #endregion

                OutInfoEntity infoEntity = new OutInfoEntity();
                ExecuteAPI exectAPI = new ExecuteAPI();
                exectAPI.CreateNDOEntity(cdoName, inputParaEntity, ref infoEntity);

                if (infoEntity.OutFlag == 1)
                {
                    ds = m_DAL.GetNDOTable(cdoName, cdoFieldName, name);
                    toolID = ds.Tables[0].Rows[0][cdoFieldID].ToString();
                }
            }
            return toolID;
        }

    }
}
